﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrazyTalk.BLL;
using CrazyTalk.Common;
using CrazyTalk.Models;
using CrazyTalk.Web.UI;
using CrazyTalk.BLL;
namespace CrazyTalk.Web.Admin
{
    public partial class Menus : UserPage
    {
        /// <summary>
        /// 适配一个页面挂了多个菜单ID
        /// </summary>
        public List<int> MenuIdList { get; set; }
        /// <summary>
        /// 控制菜单展开变量，在每个页面中都要赋值菜单ID
        /// </summary>
        public int MenuIDForExpand { get; set; }
        private int _menuid = -1;
        /// <summary>
        /// 编号为-1的，默认任何用户都拥有权限
        /// </summary>
        public int MenuId
        {
            get { return _menuid; }
            set { _menuid = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string methodName = Request["method"];
                if (string.IsNullOrEmpty(methodName))
                {
                    methodName = Form("method");
                }
                Type type = GetType();
                MethodInfo method = type.GetMethod(methodName);
                if (method == null)
                    throw new Exception("method is null");
                method.Invoke(this, null);
            }
            catch (Exception ex)
            {
                SakForNet.LoggerManager.GetLogger().Error(ex, "Menus.aspx");
                Response.Write(JSON.GetJsonMessage(false, "处理错误"));
            }
        }
        /// <summary>
        /// 将菜单层次显示转换为Html内容
        /// </summary>
        /// <returns>转换后的Html内容</returns>
        public void GetMenuListHtml()
        {
        }
        public void GetMenuList()
        {
            if (CurUserInfo == null)
            {
                Response.Write(JSON.GetJsonMessage(false, "登陆状态失效"));
                return;
            }
            IList<MenuNew> list = MenuBLL.Instance.GetMenuListByCache(CurUserInfo.UserId, CurUserInfo.RoleId);
            if (list != null && list.Count > 0)
            {
                #region 组织菜单数据
                List<MenuNew> pMenu = list.Where(o => o.ParentId == -1).ToList();
                if (pMenu.Count == 0)
                    return;
                StringBuilder sbHtml = new StringBuilder();
                int defalutMenuId = list[0].MenuID;
                if (MenuIdList != null && MenuIdList.Count > 0)
                {
                    List<MenuNew> cls = list.Where(o => MenuIdList.Contains(o.MenuID)).ToList();
                    if (MenuIdList.Count == 1 && MenuIdList[0] == -1 && MenuIDForExpand > 0)
                    {
                        cls = list.Where(o => o.MenuID == MenuIDForExpand).ToList();
                    }
                    if (cls.Count != 0)
                    {
                        defalutMenuId = cls[0].ParentId;
                    }
                    if (defalutMenuId == -1 || defalutMenuId == 0)
                        defalutMenuId = 0;
                }
                #endregion
                StringBuilder sb = new StringBuilder();
                foreach (MenuNew pitem in pMenu)
                {
                    sb.Append(string.Format("<div title=\"{0}\" style=\"padding: 5px;\">", string.IsNullOrEmpty(pitem.Alias) ? pitem.MenuName : pitem.Alias));
                    List<MenuNew> curMenu = list.Where(o => o.ParentId == pitem.MenuID).ToList();
                    if (curMenu.Count == 0)
                    {
                        sb.Append("</div>");
                        continue;
                    }
                    sb.Append("<ul class=\"easyui-tree\">");
                    foreach (MenuNew childItem in curMenu)
                    {
                        sb.Append("<li>");
                        sb.Append(string.Format("<a href=\"javascript:void(0);\" src=\"{0}\" class=\"{1}\">{2}</a></li>"
                                                , childItem.MenuUrl, "cs-navi-tab", string.IsNullOrEmpty(childItem.Alias) ? childItem.MenuName : childItem.Alias));
                    }
                    sb.Append("</ul>");
                    sb.Append("</div>");
                }
                Response.Write(JSON.GetJsonMessage(true, sb.ToString()));
            }
            else
            {
                Response.Write(JSON.GetJsonMessage(true, "<div>无菜单</div>"));
            }
        }
    }
}